# -*- coding: utf-8 -*-
'''
用户API
要实现的接口：
taobao.user.get 获取单个用户信息
taobao.users.get 获取多个用户信息
'''
from TopAPI.lib import *
import json

fields='user_id,uid,nick,sex,buyer_credit,\
    seller_credit,location,created,last_visit,birthday,\
    type,has_more_pic,item_img_num,item_img_size,prop_img_num,\
    prop_img_size,auto_repost,promoted_type,status,alipay_bind,\
    consumer_protection,alipay_account,alipay_no,avatar,liangpin,\
    sign_food_seller_promise,has_shop,is_lightning_consignment,has_sub_stock,is_golden_seller,\
    vip_info,email,magazine_subscribe,vertical_market,online_gaming'

def save_user(j):
	db=getConn('TOPAPI')
	
	#save user
	db.query('DESCRIBE USER')
	r=db.store_result()
	vs=list()
	for i in range(r.num_rows()):
		field=r.fetch_row()[0][0]
		if j.has_key(field):vs.append("'%s'"%j[field])
		else:vs.append('NULL')
	vs[-1]='NOW()'
	cmdStr="INSERT INTO USER VALUES(%s)"%(','.join(vs))
	db.query(cmdStr.encode('utf-8'))
	
	#save user_credit
	db.query('DESCRIBE USERCREDIT')
	r=db.store_result()
	vs=list()
	for i in range(r.num_rows()):
		field=r.fetch_row()[0][0]
		if j['buyer_credit'].has_key(field):vs.append("'%s'"%j['buyer_credit'][field])
		else:vs.append('NULL')
	vs[-1]='NOW()'
	cmdStr="INSERT INTO USERCREDIT VALUES(%s)"%(','.join(vs))
	db.query(cmdStr.encode('utf-8'))
	
	db.commit()
	db.close()
	pass



class user():
    api='taobao.user.get'
    def __init__(self):
        self.method=self.api
        self.setParams()
    def setParams(self,session=None,format='json',nick=''):
        self.__dict__.update(locals())
    def getRes(self):
        d={k:v for k,v in self.__dict__.items() if k!='self' and v!=None}
        d['fields']=fields
        url=getURL(d)
        return getRes(url)
    def parseUser(self):
        r=self.getRes()
        j=json.loads(r.content,r.encoding)
        #if not j.has_key('user_get_response'):j=None
        #else:j=j['user_get_response']['user']
        return parseJsonField(j,'user')
        pass
    pass

class users():
    api='taobao.users.get'
    def __init__(self):
        self.method=self.api
    def setParams(self,format='json',nicks=''):
        self.__dict__.update(locals())
    def getRes(self):
        d={k:v for k,v in self.__dict__.items() if k!='self' and v!=None}
        d['fields']=fields
        url=getURL(d)
        return getRes(url)
    def parseUsers(self):
        r=self.getRes()
        j=json.loads(r.content,r.encoding)
        '''
        if not j.has_key('users_get_response'):j=None
        else:j=j['users_get_response']['users']['user']
        '''
        return parseJsonField(j,'user')
    pass

def main():
    u=user()
    u.setParams(nick='osa品牌服饰旗舰店')
    j=u.parseUser()
    #print j
    printJson(j)
    #save_user(j)
    u=users()
    u.setParams(nicks='osa品牌服饰旗舰店,geil旗舰店')
    for usr in u.parseUsers():
        #print usr
        printJson(usr)
        #save_user(usr)
    pass

if __name__=='__main__':
    main()